package com.zz.framework.core.widget;

import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zz.framework.core.widget.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.zz.framework.R;

import androidx.recyclerview.widget.RecyclerView;

public class SuperRefreshManager {
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mRefreshLayout;
    private View mEmptyView;
    private Button mBtnEmpty;
    private ImageView mEmptyIcon;

    public ImageView getEmptyIcon() {
        return mEmptyIcon;
    }

    public SuperRefreshManager(){

    }
    public SuperRefreshManager(View view){
        init(view);
    }


    public void init(Activity view) {
        this.mRefreshLayout = view.findViewById(R.id.refreshLayout);
        initView();
    }

    public void init(View view) {
        this.mRefreshLayout = view.findViewById(R.id.refreshLayout);
        initView();
    }

    private void initView() {
        if (this.mRefreshLayout != null) {
            mRefreshLayout.setEnableLoadMoreWhenContentNotFull(true);
            mRecyclerView = this.mRefreshLayout.findViewById(R.id.recyclerView);
            mEmptyView = this.mRefreshLayout.findViewById(R.id.viewEmpty);
            setEmptyVisibility(View.GONE);
            setEnableRefresh(false);
            setEnableLoadMore(false);
            //重新加载
            if(mEmptyView!=null){
                mEmptyIcon=mEmptyView.findViewById(R.id.icon);
//                mBtnEmpty=mEmptyView.findViewById(R.id.btnEmpty);
//                mBtnEmpty.setVisibility(View.VISIBLE);
//                mBtnEmpty.setText("重新加载");
//                mBtnEmpty.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        setEmptyVisibility(View.GONE);
//                        mRefreshLayout.autoRefresh();
//                    }
//                });
            }
        }
    }

    public void setEmptyIcon(int mEmptyIconRes){
        if (mEmptyIcon!=null){
            mEmptyIcon.setImageResource(mEmptyIconRes);
        }
    }


    public void setLoadDefault(boolean loadDefault) {

    }

    public void setEnableEmptyButton(boolean enableEmptyButton) {
        if(mBtnEmpty==null)return;
        if(enableEmptyButton){
            mBtnEmpty.setVisibility(View.VISIBLE);
        }else {
            mBtnEmpty.setVisibility(View.GONE);
        }
    }
    public void autoRefresh(){
        setEmptyVisibility(View.GONE);
        mRefreshLayout.autoRefresh(0);
    }

    public void setDefaultHead() {
        if (mRefreshLayout != null)
            mRefreshLayout.setRefreshHeader(new ClassicsHeader(mRefreshLayout.getContext()));
    }

    public void setDefaultFooter() {
        if (mRefreshLayout != null)
            mRefreshLayout.setRefreshFooter(new ClassicsFooter(mRefreshLayout.getContext()));
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        if (mRefreshLayout != null && onRefreshListener != null)
            mRefreshLayout.setOnRefreshListener(onRefreshListener);
    }

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        if (mRefreshLayout != null && onLoadMoreListener != null) {
            mRefreshLayout.setOnLoadMoreListener(onLoadMoreListener);
        }
    }

    public void addItemDecoration(RecyclerView.ItemDecoration itemDecoration) {
        if (mRecyclerView != null && itemDecoration != null) {
            mRecyclerView.addItemDecoration(itemDecoration);
        }
    }

    public void addDefaultItemDecoration() {
        if (getRecyclerView().getContext() != null)
            addItemDecoration(new HorizontalDividerItemDecoration.Builder(getRecyclerView().getContext())
                    .colorResId(R.color.color_divider).size(1)
                    .showLastDivider().build());
    }
    public void addDefaultItemDecoration(int size) {
        if (getRecyclerView().getContext() != null)
            addItemDecoration(new HorizontalDividerItemDecoration.Builder(getRecyclerView().getContext())
                    .colorResId(R.color.color_divider).size(size)
                    .showLastDivider().build());
    }
    public void addBackgroundBgItemDecoration(int size) {
        if (getRecyclerView().getContext() != null)
            addItemDecoration(new HorizontalDividerItemDecoration.Builder(getRecyclerView().getContext())
                    .colorResId(R.color.background_color).size(size)
                    .showLastDivider().build());
    }

    public void addDefaultItemDecoration(int size,int color) {
        if (getRecyclerView().getContext() != null)
            addItemDecoration(new HorizontalDividerItemDecoration.Builder(getRecyclerView().getContext())
                    .colorResId(color).size(size)
                    .showLastDivider().build());
    }


    public void setLayoutManager(RecyclerView.LayoutManager layout) {
        if (mRecyclerView != null) {
            mRecyclerView.setLayoutManager(layout);
        }
    }


    public void setAdapter(RecyclerView.Adapter adapter) {
        if (mRecyclerView != null && adapter != null) {
            mRecyclerView.setAdapter(adapter);
        }
    }

    private boolean isEnable = false;
    public void setEnableLoadMore(boolean isEnable) {
        this.isEnable =true;
        if (mRefreshLayout != null) {
            mRefreshLayout.setEnableLoadMore(isEnable);
        }
    }

    public boolean isEnableLoadMore(){
        return isEnable;
    }

    public void setEnableRefresh(boolean isEnable) {
        if (mRefreshLayout != null) {
            mRefreshLayout.setEnableRefresh(isEnable);
        }
    }

    public void finishRefresh() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefresh(0);
        }
    }
    public void finishRefresh(int delayed) {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishRefresh(delayed);
        }
    }
    public void finish() {
        finishRefresh();
        finishLoadMore();
    }
    public void finishLoadMore() {
        if (mRefreshLayout != null) {
            mRefreshLayout.finishLoadMore(0);
        }
    }


    public void setEmptyVisibility(int visibility) {
        if (mEmptyView != null) {
            mEmptyView.setVisibility(visibility);
            if (mRecyclerView != null && visibility == View.GONE) {
                mRecyclerView.setVisibility(View.VISIBLE);
            } else if (mRecyclerView != null && visibility == View.VISIBLE) {
                mRecyclerView.setVisibility(View.GONE);
            }
        }
    }

    public void setEmptyVisibility(boolean visibility) {
        if (mEmptyView != null) {
            mEmptyView.setVisibility(visibility ? View.VISIBLE : View.GONE);
            if (mRecyclerView != null && !visibility) {
                mRecyclerView.setVisibility(View.VISIBLE);
            } else if (mRecyclerView != null && visibility) {
                mRecyclerView.setVisibility(View.GONE);
            }
        }
    }

    public void setEmptyString(String emptyString) {
        if (mEmptyView != null) {
//            TextView textView = mEmptyView.findViewById(R.id.title);
            TextView textView = mEmptyView.findViewById(R.id.empty);
            if (textView != null) {
                textView.setText(emptyString);
            }
        }
    }

    public RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    public View getEmptyView() {
        return mEmptyView;
    }

    public SmartRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }
}
